var format = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memu: ["个人资料","才艺展示","设置"],
    memuIndex: 0,
    basic: "",
    aboutList: "",
    //birthday: format.formatDate(new Date())
    birthday: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var basicId = options.cardId;
    console.log("接收到的个人id为:"+basicId);
    var url = app.globalData.serverAddress + "/api/personal/oneById?id=" + basicId + "&_t=" + new Date().getTime();
    app.httpPost(url,"").then((res) =>{
      if(res.data.flag){
        console.log("查询成功");
        var basic = res.data.basic;
        var bir = basic.birthday;
        this.setData({
          basic: basic,
          aboutList: res.data.aboutList,
          birthday: bir == null ? format.formatDate(new Date()) : format.formatDate(new Date(bir)),
        });
      }else{
        wx.showModal({
          content: '网络异常111',
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      wx.showModal({
        content: '网络异常222',
        showCancel: false,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
  },

  //  点击日期组件确定事件  
  bindDateChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      birthday: e.detail.value
    })
  },
  /**
   * 表单提交
   */
  formSubmit: function(e) {
    var data = e.detail.value;
    console.log(data);
    var url = app.globalData.serverAddress + "/api/personal/card/update?_t=" + new Date().getTime();
    app.httpPost(url,data).then((res) =>{
      if(res.data.flag){
        console.log("修改成功!");
        console.log("传递的openid为:"+res.data.wxOpenId);
        wx.showToast({
          title: "修改成功",
        })
      }else{
        wx.showModal({
          content: '网络异常',
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      wx.showModal({
        content: '网络异常',
        showCancel: false,
      })
    })

    /**
     * 字段非空校验
     */
    // if (!phoneNumber || !email) {
    //   app.toastShow(that,'手机或邮箱为空','');
    //   console.log(123);
    //   return;
    // }
    // app.httpPost(url, data).then((res) => {
    //   if(res.data.flag){
    //     app.toastShow(that, res.data.msg, '');
    //     wx.navigateTo({
    //       url: '/pages/personal/view-card/view-card?cardId=' + res.data.cardId + '&_t=' + new Date().getTime(),
    //     })
    //   }else{
    //     wx.showModal({
    //       content: '网络异常',
    //       showCancel: false,
    //     })
    //   }
    // }).catch((errMsg) => {
    //   wx.showModal({
    //     content: '网络异常',
    //     showCancel: false,
    //   })
    // })
  },
  /**
   * 导航触发事件
   */
  changeMemu: function(e){
    var index = e.currentTarget.dataset.index;
    console.log(index);
    this.setData({
      memuIndex: index,
    });
  },
  /**
   * 点击添加才艺
   */
  artAdd: function(){
    wx.navigateTo({
      url: "/pages/personal/about-card/about-card?personalId=" + this.data.basic.id + "&_t=" + new Date().getTime(),
    });
  }
})